package com.itheima.steawmdemo;

import java.util.ArrayList;
import java.util.Arrays;

/*
    创建一个集合，存储多个字符串元素
    "张三丰","张无忌","张翠山","王二麻子","张良","谢广坤"
    把集合中所有以“张"开头的元素存储到一个新的集合
    把"张"开头的集合中长度为3的元素存储到一个新的集合
    遍历上一步得到的集合
 */
public class StreamDemo1 {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>(Arrays.asList("张三丰", "张无忌", "张翠山", "王二麻子", "张良", "谢广坤"));

        ArrayList<String> list2 = new ArrayList<>();
        for (String s : list1) {
            if (s.startsWith("张")) {
                list2.add(s);
            }
        }
        System.out.println(list2);

        System.out.println("------------------------------------");

        ArrayList<String> list3 = new ArrayList<>();
        list2.forEach(s -> {
            if (s.length() == 3) {
                list3.add(s);
            }
        });
        list3.forEach(s-> System.out.println(s));

        System.out.println("------------------------------------");

        // Stream 流
        list1.stream().filter(s -> s.startsWith("张")).filter(s -> s.length() == 3).forEach(s-> System.out.println(s));
    }
}
